/** Lachlan Hewson-Doherty (h201384171)
 *  Diploma of Digital and Interactive Games
 *  lachlan_doherty@live.com.au
 *  2013
 */

package cabbage.database.operations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cabbage.model.entities.Account;
import cabbage.model.entities.Player;
import cabbage.database.utilities.DatabaseConnection;

public class PlayerOps {
	
	private DatabaseConnection db;
	
	public List<Account> getPlayerAccount() throws SQLException {
		
		db = new DatabaseConnection
								();
		
		Connection conn = db.getConnection
										();
		
		PreparedStatement ps = conn.prepareStatement
				("SELECT a.account_id,a.username,a.firstname,a.lastname,a.emailaddress,a.password,p.player_id FROM account a INNER JOIN player p ON a.account_id=p.account_id;");
		
		ResultSet rs = ps.executeQuery
									();
		
		List<Account> accountList = new ArrayList<Account>
														();
		
		while (rs.next()) {
			
			Player p = new Player
							(rs.getInt("a.account_id"),
								rs.getInt("p.player_id"));
			
			Account a = new Account
							(rs.getInt("a.account_id"),
								rs.getString("a.username"),
									rs.getString("a.password"),
										rs.getString("a.firstname"),
											rs.getString("a.lastname"),
												rs.getString("a.emailaddress"),
													p);
			
			accountList.add
						(a);
			
		}
		
		conn.close
				();
		
		return accountList;
		
	}
	
	
	public boolean checkPlayerExists(int player_id) throws SQLException {
		
		
		db = new DatabaseConnection
								();

		Connection conn = db.getConnection
										();		

		PreparedStatement ps = conn.prepareStatement
						("SELECT player_id FROM player WHERE player_id=?");

		ps.setInt
				(1, player_id);

		ResultSet rs = ps.executeQuery
									();

		if (rs.next()) {	
			
			conn.close();
			return true;

		}

		conn.close();		
		return false;
		
	}
	
	
	public boolean checkPlayerHasCollected ( int livecollectible_id, int player_id ) throws SQLException {
		
		
		
		db = new DatabaseConnection
								();

		Connection conn = db.getConnection
										();		

		PreparedStatement ps = conn.prepareStatement
						("SELECT * FROM collected WHERE livecollectible_id=? AND player_id=?");

		ps.setInt
				(1, livecollectible_id);

		ps.setInt
				(2, player_id);

		ResultSet rs = ps.executeQuery
									();

		if (rs.next()) {	
			
			conn.close();
			return true;

		}

		conn.close();		
		return false;
		
	}

}
